package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.ICarouselService;
import com.codedancer.campus.manager.service.bo.carousel.CarouselAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.carousel.CarouselPageSearchBo;
import com.codedancer.campus.manager.service.vo.carousel.CarouselPageVo;
import com.codedancer.campus.mapper.CarouselMapper;
import com.codedancer.campus.po.Carousel;
import com.codedancer.campus.qo.carousel.CarouselPageSearchQo;

import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CarouselServiceImpl implements ICarouselService {

    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 添加轮播图
     *
     * @param carouselAddOrUpdateBo
     */
    @Override
    public void add(CarouselAddOrUpdateBo carouselAddOrUpdateBo) {
        // 判断是否是启用，如果是启用则泵超过6张轮播图
        if (!carouselAddOrUpdateBo.getDisabled()) {
            if (carouselMapper.countByColumn("disabled", false) >= 6) {
                throw new BusinessException("轮播图上线不能超过6张");
            }
        }
        Carousel carousel = BeanUtil.copyProperties(carouselAddOrUpdateBo, Carousel.class);
        carousel.setUuid(UUIDGenerator.getUUID());
        carouselMapper.insert(carousel);
    }

    /**
     * 修改轮播图
     *
     * @param carouselAddOrUpdateBo
     */
    @Override
    public void update(CarouselAddOrUpdateBo carouselAddOrUpdateBo) {
        Carousel carousel = carouselMapper.findById(carouselAddOrUpdateBo.getUuid())
            .orElseThrow(() -> new BusinessException("轮播图不存在"));
        // 判断该轮播图是否启用
        if (!carouselAddOrUpdateBo.getDisabled()) {
            // 启用轮播图
            if (carousel.getDisabled()) {
                // 若之前是禁用，需要判断启用是否超过6张
                if (carouselMapper.countByColumn("disabled", false) >= 6) {
                    throw new BusinessException("轮播图上线不能超过6张");
                }
            }
        }
        carousel.setName(carouselAddOrUpdateBo.getName());
        carousel.setUrl(carouselAddOrUpdateBo.getUrl());
        carousel.setDescription(carouselAddOrUpdateBo.getDescription());
        carousel.setDisabled(carouselAddOrUpdateBo.getDisabled());
        carouselMapper.updateById(carousel);
    }

    /**
     * 删除轮播图
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        if (!carouselMapper.existsByUuId(uuid)) {
            throw new BusinessException("轮播图不存在");
        }
        carouselMapper.deleteById(uuid);
    }

    /**
     * 分页查询轮播图
     *
     * @param carouselPageSearchBo
     * @return
     */
    @Override
    public IPage<CarouselPageVo> page(CarouselPageSearchBo carouselPageSearchBo) {
        CarouselPageSearchQo carouselPageSearchQo = BeanUtil.copyProperties(carouselPageSearchBo,
            CarouselPageSearchQo.class);
        IPage<Carousel> carouselIPage = carouselMapper.page(
            new Page<Carousel>(carouselPageSearchBo.getPage(), carouselPageSearchBo.getSize()),
            carouselPageSearchQo);
        List<CarouselPageVo> carouselPageVos;
        IPage<CarouselPageVo> carouselPageVoIPage = new Page<>();
        carouselPageVos = carouselIPage.getRecords().stream().map(carousel -> {
            return BeanUtil.copyProperties(carousel, CarouselPageVo.class);
        }).collect(Collectors.toList());
        carouselPageVoIPage.setTotal(carouselIPage.getTotal());
        carouselPageVoIPage.setRecords(carouselPageVos);
        return carouselPageVoIPage;
    }

    /**
     * 启用轮播图
     *
     * @param uuid
     */
    @Override
    public void enable(String uuid) {
        Carousel carousel = carouselMapper.findById(uuid).orElseThrow(() -> new BusinessException("轮播图不存在"));
        // 判断启用的轮播图是否超过6个
        if (carouselMapper.countByColumn("disabled", false) >= 6) {
            throw new BusinessException("轮播图上线不能超过6张");
        }
        carousel.setDisabled(false);
        carouselMapper.updateById(carousel);
    }

    /**
     * 禁用轮播图
     *
     * @param uuid
     */
    @Override
    public void disable(String uuid) {
        Carousel carousel = carouselMapper.findById(uuid).orElseThrow(() -> new BusinessException("轮播图不存在"));
        carousel.setDisabled(false);
        carouselMapper.updateById(carousel);
    }
}
